package com.shop.cereshop.app.test;

import com.alibaba.fastjson.JSONObject;
import com.shop.cereshop.app.dl.logic.domian.RequestHead;
import com.shop.cereshop.app.dl.logic.service.Impl.SendPostImpl;
import com.shop.cereshop.app.dl.logic.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


/**
 * @author: 况鑫威
 * @Date: 2021/7/28
 * @Time: 11:00
 * @ClassName: test
 * @Description: com.shop.cereshop.app.test
 */
@RestController
public class test {

    @Autowired
    private RestTemplate restTemplate;
    @Value("${Dl_Data.version}")
    private String version;
    @Value("${Dl_Data.userId}")
    private String user_id;
    @Value("${Dl_Data.name}")
    private String user_key;

    /**
     *
     * @description:测试订单核销接口方法
     * @param
     * @return:
     *
     * @author:作者:YF
     * @time:日期:2021/6/25  时间:11:33
     */
    @PostMapping("/ConsumeNoticeTest")
    public String  ConsumeNoticeTest(){
        //.返回参数
        String result = new String();
        //.定义时间
        long st = System.currentTimeMillis() / 1000;
        //封装请求参数
        String string = "" + "{" +
                "orderSerialId:\"" + "564415944899814" + "\"," +
                "partnerOrderId:\"" + "1000071955" + "\"," +
                "tickets:" + 1 + "," +
                "consumeDate:\"" + "2018-07-04 12:09:40" + "\"," +
                "totalCheckCount:" + 1 +
                "}";
        //加密将json格式的业务参数进行des加密，加密模式ECB充方式 PKCS7Padding,密钥为user_key。
        String encrypt = null;
        try {
            encrypt = SendPostImpl.encrypt(string, user_key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String md5 = SendPostImpl.getMD5(user_id + "ConsumeNotice" + st + version + encrypt + user_key);
        //.封装JSON格式数据
        JSONObject json = new JSONObject();
        RequestHead requestHead = new RequestHead();
        requestHead.setUser_id(user_id);
        requestHead.setMethod("ConsumeNotice");
        requestHead.setTimestamp(st);
        requestHead.setVersion(version);
        requestHead.setSign(md5);
        json.put("requestHead", requestHead);
        json.put("requestBody", encrypt);
        //.请求地址URL
        String url = "http://kuang.fgimax.vipnps.vip/Tongcheng/ProcessRequest";
        System.out.println("请求参数json是："+json);
        //.发送请求
        String resultInfo = restTemplate.postForObject(url,json,String.class);
        try {
            result = SendPostImpl.decrypt((String) JsonUtils.toMap(resultInfo).get("responseBody"), user_key);
            System.out.println("响应结果"+result);
        } catch (Exception e) {
            System.out.println("请求失败");
            return resultInfo;
        }
        return result;
    }
}
